package com.qdlc.p2p.manage.action.additional;


import com.opensymphony.xwork2.ModelDriven;
import com.qdlc.p2p.biz.service.LinkService;
import com.qdlc.p2p.biz.web.action.BaseAction;
import com.qdlc.p2p.common.enums.EnumUploadImgType;
import com.qdlc.p2p.common.util.DateUtil;
import com.qdlc.p2p.common.util.StringUtil;
import com.qdlc.p2p.dal.common.model.jpa.PageDataList;
import com.qdlc.p2p.dal.dto.Link;
import com.qdlc.p2p.dal.model.LinkModel;
import org.apache.struts2.convention.annotation.Action;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by hongbinglin on 2016/6/3.
 */
public class ManageLinkAction extends BaseAction<LinkModel> implements ModelDriven<LinkModel> {

    @Resource
    private LinkService linkService;

    private Map<String, Object> data;

    /**
     * 友情链接列表页面
     *
     * @return
     * @throws Exception
     */
    @Action(value = "/modules/column/link/linkManager")
    public String linkManager() throws Exception {
        return "linkManager";
    }

    /**
     * 添加友情链接页
     *
     * @return
     * @throws Exception
     */
    @Action(value = "/modules/column/link/linkAddPage")
    public String linkAddPage() throws Exception {
        return "linkAddPage";
    }

    /**
     * 修改友情链接页面
     *
     * @return
     * @throws Exception
     */
    @Action(value = "/modules/column/link/linkEditPage")
    public String linkEditPage() throws Exception {
        long id = paramLong("id");
        Link link = linkService.findById(id);
        request.setAttribute("link", link);
        return "linkEditPage";
    }

    /**
     * 友情链接列表
     *
     * @throws Exception
     */
    @Action(value = "/modules/column/link/linkList")
    public void linkList() throws Exception {
        data = new HashMap<String, Object>();
        PageDataList<Link> linkList = linkService.findLinkList(model);
        data.put("total", linkList.getPage().getTotal()); // 总行数
        data.put("rows", linkList.getList()); // 集合对象
        printJson(getStringOfJpaObj(data));
    }

    /**
     * 添加友情链接
     *
     * @return
     * @throws Exception
     */
    @Action(value = "/modules/column/link/linkAdd")
    public void linkAdd() throws Exception {
        data = new HashMap<String, Object>();
        Link link = model.prototype();
        link.setAddTime(DateUtil.getNow());
        String picPath = imgUpload(EnumUploadImgType.ARTICLE.getValue());
        if (StringUtil.isNotEmpty(picPath)) {
            link.setPicPath(picPath);
        }
        linkService.save(link);
        //保存到文章点击量表
        data.put("result", true);
        data.put("msg", "保存成功！");
        printJson(getStringOfJpaObj(data));
    }

    /**
     * 修改友情链接
     *
     * @return
     * @throws Exception
     */
    @Action(value = "/modules/column/link/linkEdit")
    public void articleEdit() throws Exception {
        data = new HashMap<String, Object>();
        Link link = model.prototype();
        String picPath = imgUpload(EnumUploadImgType.ARTICLE.getValue());
        if (StringUtil.isNotEmpty(picPath)) {
            link.setPicPath(picPath);
        }
        linkService.update(link);
        data.put("result", true);
        data.put("msg", "修改成功！");
        printJson(getStringOfJpaObj(data));
    }

}
